<?php namespace App\Http\Controllers\admin\recommend;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use App\Models\Projects;
use App\Models\Techs;
use App\Models\Devices;
use App\Models\Technologies;
use App\Models\Users;
use Redirect;
use Session;




class RecommendController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        if(Session::has('admin')){
		return view("admin.recommend.index")->withIsRecommends(true);
        }else{
            return Redirect::to('admin/dologin');
        }
	}

     public function getIndexProject()
    {
    	$data = Projects::where("is_rec_industry_index", 1)->where("industry_order","!=", 0)->orderBy("industry_order", "desc")->get()->toArray();
    	return $this->json(["data" =>$data]);
    }

    public function getHotProject(){

        $data = Projects::where("is_rec_list_bottom", 1)->where("hot_order","!=",0)->orderBy("hot_order", "desc")->get()->toArray();
        return $this->json(["data" =>$data]);
    }
    public function getHotTech(){

        $data = Technologies::where("is_rec_list_bottom", 1)->where("hot_order","!=",0)->orderBy("hot_order", "desc")->get()->toArray();
        return $this->json(["data" =>$data]);
    }

    public function getHotDevice(){

        $data = Devices::where("is_rec_list_bottom", 1)->where("hot_order","!=",0)->orderBy("hot_order", "desc")->get()->toArray();
        return $this->json(["data" =>$data]);
    }


    public function getBigRecommend(){

        $data = Projects::where("is_rec_index_big", 1)->get()->toArray();
        return $this->json(["data" =>$data]);
    }

     public function getIndexDevice()
    {
    	$data = Devices::where("is_rec_industry_index", 1)->where("industry_order","!=",0)->orderBy("industry_order", "desc")->get()->toArray();
    	return $this->json(["data" => $data]);
    }

     public function getIndexTech()
    {
    	$data = Technologies::where("is_rec_industry_index", 1)->where("industry_order","!=",0)->orderBy("industry_order", "desc")->get()->toArray();
    	return $this->json(["data" => $data]);
    }

    public function getIndexGroup(){
    	$users = Users::where("validate_state", 2)->whereNotIn('role_id', [0,1,8,9,10])->where("is_rec_industry_index",1)->orderBy("industry_order","desc")->get();

		foreach ($users as $key => $value) {
			switch ($value["role_id"]) {
				case env("ENERGY"):
					$value->load("poweruser");
					$value["userData"] = $value["poweruser"]->toArray();
					unset($value["poweruser"]);
					break;
				case env("SAVER"):
					$value->load("energysaver");
					$value["userData"] = $value["energysaver"]->toArray();
					unset($value["energysaver"]);
					break;
				case env("DEVICE_TECH"):
					$value->load("devicetech");
					$value["userData"] = $value["devicetech"]->toArray();
					unset($value["devicetech"]);
					break;
				case env("THIRD"):
					$value->load("thirdparty");
					$value["userData"] = $value["thirdparty"]->toArray();
					unset($value["thirdparty"]);
					break;
				case env("ASSOCIATION"):
					$value->load("association");
					$value["userData"] = $value["association"]->toArray();
					unset($value["association"]);
					break;
				case env("GOVERNMENT");
					$value->load("government");
					$value["userData"] = $value["government"]->toArray();
					unset($value["government"]);
					break;
			}
		}

		return $this->json(["data" => $users]);

    }


    public function getOrderProject()
    {
    	$data = Projects::where("is_rec", 1)->where("order","!=",0)->orderBy("order", "desc")->get()->toArray();
    	return $this->json(["data" => $data]);
    }

    public function getIndexRecommend(){

        $data  = Projects::where("is_rec_index", 1)->where("index_order", "!=", 0)->orderBy("index_order", "desc")->get()->toArray();
        return $this->json(["data" =>$data]);
    }
    
    public function getBig(){

        $data = Projects::where("is_rec_index_big", 1)->get()->toArray();
        return $this->json(["data" =>$data]);
    }

    public function getIndex(){
        $data = Projects::where("is_rec_index", 1)->get()->toArray();
        return $this->json(["data" =>$data]);
    }

    public function getOrderUser(){

    	$data = Users::with("profile")->where("is_rec_industry_index", 1)->where("industry_order", "!=",0)->orderBy("industry_order","desc")->get()->toArray();
    	return $this->json(["data" =>$data]);
    }

    public function getOrderTech(){

    	$data = Technologies::where("is_rec", 1)->where("order","!=",0)->orderBy("order","desc")->get()->toArray();
    	return $this->json(["data" =>$data]);
    }

    public function getOrderDevice(){


    	$data = Devices::where("is_rec",1)->where("order","!=",0)->orderBy("order","desc")->get()->toArray();
    	return $this->json(["data" =>$data]);
    }

    public function getOrderGroup(){

    	$data = Users::where("is_rec",1)->where("order","!=",0)->orderBy("order","desc")->get()->toArray();
    	return $this->json(["data" =>$data]);
    }

    public function getProject(){

        $data = Projects::where("verify_state", 1)->get()->toArray();
		return $this->json(["data" => $data]);
    }
    

    public function getTechs(){

        $data = Technologies::where("verify_state",1)->get()->toArray();
        return $this->json(["data" =>$data]);
    }

    public function getUser(){

    	$data = Users::with("profile")->whereNotIn("role_id",[0,1,8,9,10])->get()->toArray();
    	return $this->json(["data" =>$data]);
    }



    public function getDevice(){

    	$data = Devices::where("verify_state", 1)->get()->toArray();
    	return $this->json(["data"=>$data]);
    }


    public function getGroup()
    {
		$users = Users::where("validate_state", 2)->whereNotIn('role_id', [0,1,8,9,10])->get();

		foreach ($users as $key => $value) {
			switch ($value["role_id"]) {
				case env("ENERGY"):
					$value->load("poweruser");
					$value["userData"] = $value["poweruser"]->toArray();
					unset($value["poweruser"]);
					break;
				case env("SAVER"):
					$value->load("energysaver");
					$value["userData"] = $value["energysaver"]->toArray();
					unset($value["energysaver"]);
					break;
				case env("DEVICE_TECH"):
					$value->load("devicetech");
					$value["userData"] = $value["devicetech"]->toArray();
					unset($value["devicetech"]);
					break;
				case env("THIRD"):
					$value->load("thirdparty");
					$value["userData"] = $value["thirdparty"]->toArray();
					unset($value["thirdparty"]);
					break;
				case env("ASSOCIATION"):
					$value->load("association");
					$value["userData"] = $value["association"]->toArray();
					unset($value["association"]);
					break;
				case env("GOVERNMENT");
					$value->load("government");
					$value["userData"] = $value["government"]->toArray();
					unset($value["government"]);
					break;
			}
		}

		return $this->json(["data" => $users]);

    }

    public function updateUser(){

    	$pid = Input::get("data");
    	$len = count($pid);
    	$model = Users::where("is_rec_industry_index", 1);
    	$model->update(["is_rec_industry_index" =>0, "industry_order" =>0]);
    	foreach ($pid as $key => $value) {
    		$model = Users::where("id",$value);
    		$model->update(["is_rec_industry_index" =>1, "industry_order" =>($len - $key)]);
    	}
    	return $this->json([]);
    }

    public function updateGroup()
    {
        $pid = Input::get("data");
        $len = count($pid);
        $model = Users::where("is_rec", 1);
        $model->update(["is_rec" =>0, "order" =>0]);
        foreach ($pid as $key => $value) {
            $model = Users::where("id",$value);
            $model->update(["is_rec" =>1, "order" =>($len - $key)]);
        }
        return $this->json([]);
    }

    public function updateIndexProject(){


    	$pid = Input::get("data");
    	$len = count($pid);
    	$model = Projects::where("is_rec_industry_index", 1);
    	$model->update(["is_rec_industry_index"=>0, "industry_order" =>0]);
    	foreach ($pid as $key => $value) {
    		$model = Projects::where("id",$value);
    		$model->update(["is_rec_industry_index" =>1, "industry_order" =>($len - $key)]);
    	}
    	return $this->json([]);
    }

    public function updateHotProject(){

        $pid = Input::get("data");
        $len = count($pid);
        $model = Projects::where("is_rec_list_bottom", 1);
        $model->update(["is_rec_list_bottom"=>0, "hot_order"=>0]);
        foreach ($pid as $key => $value) {
            $model = Projects::where("id",$value);
            $model ->update(["is_rec_list_bottom"=>1, "hot_order" =>($len - $key)]);
        }
        return $this->json([]);
    }


    public function updateHotTech(){

        $pid = Input::get("data");
        $len = count($pid);
        $model = Technologies::where("is_rec_list_bottom", 1);
        $model->update(["is_rec_list_bottom"=>0, "hot_order"=>0]);
        foreach ($pid as $key => $value) {
            $model = Technologies::where("id",$value);
            $model ->update(["is_rec_list_bottom"=>1, "hot_order" =>($len - $key)]);
        }
        return $this->json([]);
    }


    public function updateHotDevice(){

        $pid = Input::get("data");
        $len = count($pid);
        $model = Devices::where("is_rec_list_bottom", 1);
        $model->update(["is_rec_list_bottom"=>0, "hot_order"=>0]);
        foreach ($pid as $key => $value) {
            $model = Devices::where("id",$value);
            $model ->update(["is_rec_list_bottom"=>1, "hot_order" =>($len - $key)]);
        }
        return $this->json([]);
    }


    // public function updateBigRecommend(){

    //     $data = Input::get("data");
    //     $model = Projects::where("is_rec_index_big", 1);
    //     $model->update(["is_rec_index_big" =>0]);
    //     if($model->count()){
    //         $model ->update(["is_rec_index_big" =>1]);
    //         $model ->update($data);
    //          return $this->json([]);
    //     }else{
    //         return;
    //     }
     public function updateBigRecommend(){

        $pid = Input::get("data");
        $len = count($pid);
        $model = Projects::where("is_rec_index_big", 1);
        $model->update(["is_rec_index_big"=>0]);
        foreach ($pid as $key => $value) {
            $model = Projects::where("id",$value);
            $model ->update(["is_rec_index_big"=>1]);
        }
        return $this->json([]);
    }

        
   

    // public function updateIndexRecommend(){
    //     $pid = Input::get("data");
    //     $len = count($pid);
    //     $model = Projects::where("is_rec_index", 1);
    //     $model->update(["is_rec_index"=>0, "index_order"=>0]);
    //     foreach ($pid as $key => $value) {
    //         $model = Projects::where("id",$value);
    //         $model = update(["is_rec_index" =>1, "index_order" =>($len - $key)]);
    //     }
    //     return $this->json([]);
    // }
    public function updateIndexRecommend(){

        $pid = Input::get("data");
        $len = count($pid);
        $model = Projects::where("is_rec_index", 1);
        $model->update(["is_rec_index"=>0, "index_order" =>0]);
        foreach ($pid as $key => $value) {
            $model = Projects::where("id",$value);
            $model->update(["is_rec_index" =>1, "index_order" =>($len - $key)]);
        }
        return $this->json([]);
    }



    public function updateIndexTechs(){

    	$pid = Input::get("data");
    	$len = count($pid);
    	$model = Technologies::where("is_rec_industry_index", 1);
    	$model->update(["is_rec_industry_index"=>0, "industry_order" =>0]);
    	foreach ($pid as $key => $value) {
    		$model = Technologies::where("id",$value);
    		$model->update(["is_rec_industry_index" =>1, "industry_order" =>($len - $key)]);
    	}
    	return $this->json([]);
    }


    public function updateIndexDevice(){

    	$pid = Input::get("data");
    	$len = count($pid);
    	$model = Devices::where("is_rec_industry_index", 1);
    	$model->update(["is_rec_industry_index" =>0, "industry_order" =>0]);
    	foreach ($pid as $key => $value) {
    		$model = Devices::where("id",$value);
    		$model->update(["is_rec_industry_index" =>1, "industry_order" =>($len - $key)]);
    	}
    	return  $this->json([]);
    }

    public function updateIndexGroup(){

    	$pid = Input::get("data");
    	$len = count($pid);
    	$model = Users::where("is_rec_industry_index", 1);
    	$model->update(["is_rec_industry_index" =>0, "order" =>0]);
    	foreach ($pid as $key => $value) {                     
    		$model = Users::where("id",$value);
    		$model->update(["is_rec_industry_index" =>1, "order" =>($len - $key)]);
    	}
    	return $this->json([]);                     
    }



    public function updateProject(){
    	$pid = Input::get("data");
    	$len = count($pid);
    	$model = Projects::where("is_rec", 1);

    	$model->update(["is_rec" => 0, "order" => 0]);

    	foreach ($pid as $key => $value) {
    		$model = Projects::where("id", $value);
    		$model->update(["is_rec" => 1, "order" => ($len - $key)]);
    	}

    	return $this->json([]);
    }


    public function updateTechs(){

    	$pid = Input::get("data");
    	$len = count($pid);
    	$model = Technologies::where("is_rec",1);
    	$model->update(["is_rec"=>0, "order" => 0]);
    	foreach ($pid as $key => $value) {
    		$model = Technologies::where("id", $value);
    		$model->update(["is_rec" =>1, "order" =>($len - $key)]);
    	}
    	return $this->json([]);
    }

    public function updateDevice(){
    	$pid = Input::get("data");
    	$len = count($pid);
    	$model = Devices::where("is_rec",1);
    	$model->update(["is_rec"=>0, "order"=> 0]);
    	foreach ($pid as $key => $value) {
    		$model = Devices::where("id",$value);
    		$model->update(["is_rec"=>1, "order" =>($len - $key)]);
    	}
    	return $this->json([]);
    }

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
